﻿package Interface.TracksClasses
{	
	import Interface.SelectableLabel;
	import Fonts.FranclinGothicDemi;
	import GameUtils.TrackData;

	import flash.display.Sprite;
	import flash.text.TextField;	
	import flash.text.TextFormat;	
	import flash.events.Event;
	import flash.events.MouseEvent;	
	import flash.events.ProgressEvent;
	
	public class TrackLabel extends SelectableLabel
	{		
		private var FCover:Sprite;
		private var FBar:Sprite;
		private var FBackground:Sprite;
		private var FData:TrackData;
				
		public function TrackLabel(AData:TrackData):void
		{		
			var vFont:FranclinGothicDemi = new FranclinGothicDemi();
			var vFormat:TextFormat = new TextFormat(vFont.fontName);
			vFormat.size = 14;			
			vFormat.align = 'center';			
			
			FBar = new Sprite();			
			addChild(FBar);
			
			FBackground = new Sprite();			
			addChild(FBackground);
			
			FText = new TextField();						
			FText.embedFonts = true;
			FText.defaultTextFormat = vFormat;			
			FText.selectable = false;			
			FText.text = AData.Name;
			FText.height = 26;
			FText.width = 433;
			FText.textColor = 0xFFFFFF;
			addChild(FText);
			
			FBackground.graphics.beginFill(0x000000, 0.2);
			FBackground.graphics.drawRect(0, 0, width, height);
			FBackground.graphics.endFill();
			
			FBar.graphics.beginFill(0x000000, 0.4);
			FBar.graphics.drawRect(0, 0, width, height);
			FBar.graphics.endFill();
			Percent = 0;
			
			FCover = new Sprite();
			FCover.graphics.beginFill(0x000000, 0);
			FCover.graphics.drawRect(0, 0, width, height);
			FCover.graphics.endFill();
			FCover.buttonMode = true;
			FCover.useHandCursor = true;
			addChild(FCover);
			
			super();
		}
		
		public function set Data(AData:TrackData):void
		{
			FData = AData;
			FData.Track.addEventListener(ProgressEvent.PROGRESS, OnSoundLoading);
			FData.Track.addEventListener(Event.COMPLETE, OnSoundLoaded);
		}
		
		private function OnSoundLoading(e:ProgressEvent):void
		{
			Percent = e.bytesLoaded / e.bytesTotal;
		}
		
		private function OnSoundLoaded(e:Event):void
		{
			Percent = 1;
		}
		
		public function get Data():TrackData
		{
			return FData;
		}
		
		public function set Percent(APercent:Number):void
		{
			FBar.scaleX = APercent;
		}
	}	
}
